package com.source.lightfowl.idcardgrantandrecovery.controller;

import com.source.lightfowl.VO.IdCardGrantVO;
import com.source.lightfowl.basedata.dao.IdCardDao;
import com.source.lightfowl.basedata.entity.IdCard;
import com.source.lightfowl.basedata.service.IdCardService;
import com.source.lightfowl.idcardgrantandrecovery.entity.IdCardGrant;
import com.source.lightfowl.idcardgrantandrecovery.service.IdCardGrantService;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 15:02
 */
@Api(value = "id卡发放管理",tags = "id卡发放管理")
@RestController
@CrossOrigin
@RequestMapping
public class IdCardGrantController {
    @Autowired
    private IdCardGrantService idCardGrantService;

    //TO DO 1. 统计id发放的数量 已测试
    @ApiOperation(value = "统计发放id卡的数量")
    @GetMapping("/getIdCardGrantCount")
    public MyResult getCount(){
        Long sun = idCardGrantService.getCount();
        if(sun != null)
            return MyResult.build(0,"成功",sun);
        else
            return MyResult.build(1,"结果为空");
    }

    //查找全部
    @ApiOperation(value = "查询全部数据")
    @GetMapping("/idCardGrant")
    public MyResult findAll(){
        List<IdCardGrantVO> all = idCardGrantService.findAll();
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //查找 分页
    @ApiOperation(value = "分页查询（传递两个参数：页数 个数/页）")
    @GetMapping("/idCardGrantFindAllPage")
    public MyResult findAllPage(int page,int pageNum){
        Page<IdCardGrant> all = idCardGrantService.findAllPage(page,pageNum);
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //根据id查
    @ApiOperation(value = "根据id查询")
    @GetMapping("/idCardGrant/id")
    public MyResult findById(int id){
        IdCardGrantVO house = idCardGrantService.findById(id);
        return MyResult.build(0,"成功",house);
    }

    //增加 修改
    @ApiOperation(value = "增加数据")
    @PostMapping("/idCardGrant")
    @LogAnnotation(operEvent = "操作类型：添加/修改",operType = 2)
    public MyResult create(IdCardGrant idCardGrant){
        idCardGrantService.save(idCardGrant);
        return MyResult.build(0,"成功");
    }

    //修改
    @ApiOperation(value = "修改 数据(只能修改除了领取人之外的信息)")
    @PostMapping("/idCardGrant/update")
    @LogAnnotation(operEvent = "操作类型：添加/修改",operType = 2)
    public MyResult update(IdCardGrant idCardGrant){
        idCardGrantService.update(idCardGrant);
        return MyResult.build(0,"成功");
    }
    //删除
    @ApiOperation("根据id删除数据")
    @DeleteMapping("/idCardGrant/id")
    @LogAnnotation(operEvent = "操作类型：删除记录",operType = 3)
    public MyResult deleteById(int id){
        idCardGrantService.deleteById(id);
        return MyResult.build(0,"删除成功");

    }

    //删除 批量
    @ApiOperation(value = "批量删除数据：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/idCardGrantDeleteBatch")
    @LogAnnotation(operEvent = "操作类型：批量删除记录",operType = 4)
    public MyResult deleteBatch(String ids){    //注意：参数的分隔符逗号是英文的
        idCardGrantService.deleteBatch(ids);
        return MyResult.build(0,"删除成功");

    }


}
